﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using WW.NActiveRecord.Internal;
using WW.NActiveRecord.Internal.Utils;

namespace WW.NActiveRecord
{
    
    public class MemberRef<T> : IEntityMember where T : Base<T>, new()
    {
        private readonly string foreignKey;

        InertInitObject<T> value;
        EntityMember entityMember = new EntityMember();

        internal MemberRef(string foreignKey){
            this.foreignKey = foreignKey;
            value = new InertInitObject<T>(() => Base<T>.Find(entityMember.Owner.Record[this.foreignKey]));
        }

        public T Value{
            get { return value.Get(); }
        }

        public void Init(FieldInfo fieldInfo, Entity owner){
            entityMember.Init(fieldInfo,owner);
        }
    }
}
